/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:07  
 * Copyright (c) 2015- -版权所有     
 */

package com.joysuch.wwyt.edu.service.impl;

import javax.transaction.Transactional;

import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.edu.bean.EduCourseCoursewareDetailBean;
import com.joysuch.wwyt.edu.bean.EduCourseCoursewareEditBean;
import com.joysuch.wwyt.edu.bean.EduCourseCoursewareListBean;
import com.joysuch.wwyt.edu.bean.EduCourseCoursewareListBean.SearchEduCourseCourseware;
import com.joysuch.wwyt.edu.entity.EduCourseCourseware;
import com.joysuch.wwyt.edu.entity.dsl.QEduCourseCourseware;
import com.joysuch.wwyt.edu.repository.EduCourseCoursewareDao;
import com.joysuch.wwyt.edu.service.EduCourseCoursewareService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class EduCourseCoursewareServiceImpl implements EduCourseCoursewareService {

    @Autowired
    private EduCourseCoursewareDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Override
    public EduCourseCourseware add(EduCourseCoursewareEditBean entity) {
        EduCourseCourseware content = new EduCourseCourseware();
        BeanUtils.copyProperties(entity, content, "id");
        return dao.save(content);
    }

    @Override
    public void update(EduCourseCoursewareEditBean entity) {
        EduCourseCourseware content = dao.findById(entity.getId()).get();
        BeanUtils.copyProperties(entity, content, "id");
        dao.save(content);
    }

    @Override
    public Page<EduCourseCoursewareListBean> findByPage(Pageable page, SearchEduCourseCourseware condition) {
        Predicate predicate = prepareListSearchCondition(condition);

        Page<EduCourseCourseware> data = predicate == null ? dao.findAll(page) : dao.findAll(predicate, page);

        return PageDataUtils.convertPageData(data,
                new PageDataConvert<EduCourseCoursewareListBean, EduCourseCourseware>() {

                    @Override
                    public EduCourseCoursewareListBean convert(EduCourseCourseware f) {
                        EduCourseCoursewareListBean bean = new EduCourseCoursewareListBean();
                        BeanUtils.copyProperties(f, bean);
                        return bean;
                    }

                });
    }

    private Predicate prepareListSearchCondition(SearchEduCourseCourseware condition) {
        Predicate p = null;
        QEduCourseCourseware q = QEduCourseCourseware.eduCourseCourseware;
        if (condition.getCourseId() != null) {
            p = ExpressionUtils.and(p, q.courseId.eq(condition.getCourseId()));
        }
        return p;
    }

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     * 
     * @param id
     *            要删除的业务ID
     * @throws BussinessRestrictionsException
     *             抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {

    }

    @Override
    public EduCourseCourseware findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public EduCourseCoursewareDetailBean getById(Long id) {
        EduCourseCourseware entity = findById(id);
        if (entity == null) {
            return null;
        }
        EduCourseCoursewareDetailBean bean = new EduCourseCoursewareDetailBean();
        BeanUtils.copyProperties(entity, bean);
        return bean;
    }

    @Override
    public int deleteByCoursewareId(Long id) {
        return dao.deleteByCoursewareId(id);
    }

    @Override
    public int deleteByCourseId(Long id) {
        return dao.deleteByCourseId(id);
    }

}
